package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;

public class SysInfo {

	public static final String dbName = "UserDB";
	public static final String collection = "SysInfo";
	
	private ObjectId _id;
	
	//用户id
	private String user_id;
	//邮箱
	private String email;
	//主题
	private String theme;
	//内容
	private String content;
	//发送时间
	private String senddate;
	//操作员id
	private String operator_id;
	
	
	
	public SysInfo(String user_id, String email, String theme, String content,
			String senddate, String operator_id) {
		super();
		this.user_id = user_id;
		this.email = email;
		this.theme = theme;
		this.content = content;
		this.senddate = senddate;
		this.operator_id = operator_id;
	}

	@Override
	public String toString() {
		return "SysInfo [_id=" + _id + ", user_id=" + user_id + ", email="
				+ email + ", theme=" + theme + ", content=" + content
				+ ", senddate=" + senddate + ", operator_id=" + operator_id
				+ "]";
	}
	
	@JsonIgnore
	public ObjectId get_id() {
		return _id;
	}

	public void set_id(ObjectId _id) {
		this._id = _id;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getSenddate() {
		return senddate;
	}

	public void setSenddate(Date senddate) {
		this.senddate = Format.date2String(senddate, 3);
	}

	public String getOperator_id() {
		return operator_id;
	}

	public void setOperator_id(String operator_id) {
		this.operator_id = operator_id;
	}

	
	public String getUsername() {
		
		User user = new User();
		user = user.getUserByUser_id(this.user_id);
		return user.getUsercode();
	}
	
	public String getOperator() {
		
		User user = new User();
		user = user.getUserByUser_id(this.operator_id);
		return user.getUsercode();
	}
	
	
	public SysInfo() {
		// TODO Auto-generated constructor stub
	}
	
	public Document toDocument(){
		return new Document("user_id", user_id).append("email", email)
				.append("theme", theme).append("content", content)
				.append("senddate", senddate).append("operator_id", operator_id);
	}
	
	
	public SysInfo(Document doc) {
		
		this._id = doc.getObjectId("_id");
		user_id = doc.getString("user_id");
		email = doc.getString("email");
		theme = doc.getString("theme");
		content = doc.getString("content");
		senddate = doc.getString("senddate");
		operator_id = doc.getString("operator_id");
		
	}
	
	/**
	 * 用于测试时向数据库中写入数据
	 */
	public void test(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
					
		User admin = new User();
		admin = admin.getUserByUserCode("admin");
		
		User user = new User();
		user = user.getUserByUserCode("user");
		SysInfo info = new SysInfo(user.get_id(), "123456789@qq.com", "角色更改通知", user.getUsername() + "，您的角色已经变更为普通用户，若想继续使用本系统完整功能请续费", Format.date2String(new Date(), 3), admin.get_id());
		Document t = info.toDocument();
		db.insert(t);
		
		info = new SysInfo(user.get_id(), "123456789@qq.com", "注册通知", user.getUsername() + "，您于XXXX年XX月XX日注册为本系统用户，欢迎使用。", Format.date2String(new Date(), 3), admin.get_id());
		t = info.toDocument();
		db.insert(t);
		
		user = user.getUserByUserCode("user_pay");
		info = new SysInfo(user.get_id(), "123456789@qq.com", "注册通知", user.getUsername() + "，您于XXXX年XX月XX日注册为本系统用户，欢迎使用。", Format.date2String(new Date(), 3), admin.get_id());
		t = info.toDocument();
		db.insert(t);
		
		
		db.closeConnection();
		
	}
	
	/**
	 * 获取 SysInfo 表中数据条数
	 * @return
	 */
	@JsonIgnore
	public long getCount(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		long count = db.collectionGetCount();
		db.closeConnection();
		return count;
		
	}
	
	
}
